#include "bitwiseanalyser.h"
#include <iostream>
#include <iomanip>

BitwiseAnalyser::BitwiseAnalyser(const void* buffer, int length)
{
    const unsigned char* buf = reinterpret_cast<const unsigned char*>(buffer);
    _buffer.assign(buf, buf + length);
}

void BitwiseAnalyser::printChars()
{
    for(std::vector<unsigned char>::const_iterator it = _buffer.begin(); it != _buffer.end(); ++it)
        std::cout << *it << ' ';
    std::cout << std::endl;
}

void BitwiseAnalyser::printHex()
{
    for(std::vector<unsigned char>::const_iterator it = _buffer.begin(); it != _buffer.end(); ++it)
        std::cout << std::hex << static_cast<int>(*it) << ' ';
    std::cout << std::endl;
}

void BitwiseAnalyser::printReverseHex()
{
    for(std::vector<unsigned char>::const_reverse_iterator it = _buffer.rbegin(); it != _buffer.rend(); ++it)
        std::cout << std::hex << static_cast<int>(*it) << ' ';
    std::cout << std::endl;
}

void BitwiseAnalyser::flip16()
{
    using namespace std;
    int size = _buffer.size()/2;
    short* buf16 = reinterpret_cast<short*>(&_buffer[0]);
    for(int i = 0; i < size; ++i)
    {
        std::cout << "buf16=" << hex << buf16[i] << std::endl;

        short temp = (buf16[i] & 0xF000 >> 2);
        std::cout << "temp=" << hex << temp << std::endl;

//        char* bytes = reinterpret_cast<char*>(buf16[i]);
//        std::cout << "before flip: bytes[0]=" << hex << bytes[0] << ", bytes[1]=" << hex << bytes[1] << std::endl;
        /*char byte = bytes[0];
        bytes[0] = bytes[1];
        bytes[1] = byte;
        std::cout << "after flip: bytes[0]=" << hex << (int)bytes[0] << ", bytes[1]=" << hex << (int)bytes[1] << "\n";
        buf16[i] = *reinterpret_cast<short*>(bytes);
        std::cout << "buf16=" << hex << (int)buf16[i] << "\n";*/
    }
}
